<template>
  <div class="login-container">
    <div class="bg">
      <h4 class="title">重置密码</h4>
      <el-form
        :model="ruleForm"
        v-show="ruleForm.isShow"
        status-icon
        :rules="rules"
        ref="ruleForm"
        label-width="100px"
        label-position="top"
      >
        <el-form-item label="用户名" prop="loginName">
          <el-input
            v-model="ruleForm.loginName"
            placeholder="请输入用户名"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="验证" prop="dragSuccess">
          <div class="step"></div>
          <div class="block">
            <el-slider
              v-model="ruleForm.value"
              @change="change"
              :show-tooltip="false"
              :class="ruleForm.class"
            >
            </el-slider>
          </div>

          <el-input
            v-model="ruleForm.dragSuccess"
            ref="dragSuccess"
            autocomplete="off"
            v-show="false"
          ></el-input>
        </el-form-item>

        <el-form-item class="mt-40">
          <el-button type="success" @click="submitForm('ruleForm')"
            >下一步</el-button
          >
          <el-button @click="back">返回</el-button>
        </el-form-item>
      </el-form>
      <el-form
        :model="passForm"
        v-show="passForm.isShow"
        status-icon
        :rules="rules"
        ref="passForm"
        label-width="100px"
        label-position="top"
      >
        <el-form-item label="姓名" prop="username">
          <el-input
            v-model="passForm.username"
            placeholder="请输入姓名"
            autocomplete="off"
          ></el-input>
        </el-form-item>
        <el-form-item label="身份证号" prop="identity">
          <el-input
            v-model="passForm.identity"
            placeholder="请输入身份证号"
            autocomplete="off"
          ></el-input>
        </el-form-item>

        <el-form-item class="mt-40">
          <el-button type="success" @click="passSubmit()">下一步</el-button>
          <el-button @click="back1">返回</el-button>
        </el-form-item>
      </el-form>
      <el-form
        :model="repwdForm"
        v-show="repwdForm.isShow"
        :rules="rules"
        ref="repwdForm"
        label-width="100px"
        label-position="top"
      >
        <el-form-item label="新密码" prop="newpwd">
          <el-input
            v-model="repwdForm.newpwd"
            placeholder="8-18位，大写字母、小写字母、数字、特殊字符中的2种"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="confirmpwd">
          <el-input
            v-model="repwdForm.confirmpwd"
            placeholder="请重复输入您的新密码"
            autocomplete="off"
            show-password
          ></el-input>
        </el-form-item>

        <el-form-item class="mt-40">
          <el-button
            type="success"
            :loading="repwdForm.loading"
            @click="reSubmit()"
            >确定</el-button
          >
          <el-button @click="back2">返回</el-button>
        </el-form-item>
      </el-form>
    </div>
    <div class="footer">
      奥鹏教育 © 2021 All Rights Reserved. 京ICP备05014615号-28
      京公网安备11010802031206号
    </div>
  </div>
</template>
<script >
import { validPassword } from "@/utils/validate.js"

export default {
  data () {
    var validatePass2 = (rule, value, callback) => {
      if (value === '') {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.repwdForm.newpwd) {
        callback(new Error('两次输入密码不一致!'));
      } else {
        callback();
      }
    };
    return {
      ruleForm: {
        isShow: true,
        loginName: "",
        dragSuccess: "",
        class: '',
        value: 0,
        token: ''
      },
      passForm: {
        isShow: false,
        username: "",
        identity: "",
        token: '',
      },
      repwdForm: {
        isShow: false,
        newpwd: '',
        confirmpwd: '',
        loading: false
      },
      rules: {
        loginName: [
          { required: true, message: "请输入您的用户名", trigger: "blur" },
        ],
        dragSuccess: [
          { required: true, message: "请先拖动滑块", trigger: "blur" },
        ],
        username: [
          { required: true, message: "请输入您的姓名", trigger: "blur" },
        ],
        identity: [
          { required: true, message: "请输入身份证号", trigger: "blur" },
        ],
        newpwd: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { validator: validPassword, trigger: 'blur' }
        ],
        confirmpwd: [
          { required: true, message: "请输入再次输入新密码", trigger: "blur" },
          { validator: validatePass2, trigger: 'blur' }
        ],
      },
    };
  },
  mounted () {
  },
  methods: {
    change () {
      console.log(this.ruleForm.value)
      if (this.ruleForm.value > 99) {
        this.ruleForm.dragSuccess = "true";
        this.ruleForm.class = "ok"
      } else {
        this.ruleForm.dragSuccess = "";
        this.ruleForm.class = ""
      }

    },
    submitForm () {
      this.$refs.ruleForm.validate(async (valid) => {
        if (valid) {
          let res = await this.axios.validusername({ username: this.ruleForm.loginName });
          if (res.success) {
            this.ruleForm.token = res.instance;
            this.$message({ type: 'success', message: '用户帐号验证通过' });
            this.ruleForm.isShow = false;
            this.passForm.isShow = true;
          } else {
            this.$message({ type: 'error', message: res.errorMsg });
          }
        }
      });
    },
    back () {
      window.location.href = '/login';
    },
    passSubmit () {
      this.$refs.passForm.validate(async (valid) => {
        if (valid) {
          let res = await this.axios.validuser({ realname: this.passForm.username, identity: this.passForm.identity, code: this.ruleForm.token });
          if (res.success) {
            this.passForm.token = res.instance;
            this.$message({ type: 'success', message: '用户信息验证通过' });
            this.passForm.isShow = false;
            this.repwdForm.isShow = true;
          } else {
            this.$message({ type: 'error', message: res.errorMsg });
          }
        }
      });
    },
    back1 () {
      this.ruleForm.value = 0;
      this.ruleForm.dragSuccess = "";
      this.ruleForm.class = ""
      this.ruleForm.isShow = true;
      this.passForm.isShow = false;
    },
    reSubmit () {
      this.$refs.repwdForm.validate(async (valid) => {
        if (valid) {
          let res = await this.axios.forgetpwd({ newPassword: this.repwdForm.newpwd, confirePassword: this.repwdForm.confirmpwd, code: this.passForm.token });
          if (res.success) {
            this.repwdForm.loading = true;
            this.$message({ type: 'success', message: '密码重置成功，请重新登录' });
            setTimeout(() => {
              window.location.href = "/login";
            }, 2000);
          } else {
            this.$message({ type: 'error', message: res.errorMsg });
          }
        }
      });
    },
    back2 () {
      this.passForm.isShow = true;
      this.repwdForm.isShow = false;
    },
  },
};
</script>

<style lang="scss" scope>
.login-container {
  height: 100%;
  width: 100%;
  padding-top: 150px;
  background-color: #f2f6fa;
  .bg {
    width: 470px;
    height: 540px;
    margin: 0 auto;
    background: #ffffff;
    padding: 48px;
    border-radius: 0px 16px 16px 0px;
    .step {
      width: 100%;
      height: 40px;
      top: -4px;
      position: absolute;
      background-color: #17c289;
    }
    .block {
      margin-top: -20px;
    }
    .el-slider__runway {
      width: 327px;
      height: 40px;
      background: #909399;
      border-radius: 0px;
      float: right;
    }
    .el-slider__bar {
      height: 40px;
      background-color: #17c289;
    }
    .el-slider__button-wrapper {
      width: 50px;
      height: 40px;
      border-radius: 0px;
      border-radius: 0px;
      margin: 15px 0px 0px -25px;
      background-color: #fff;
      border: 1px solid #c0c4cc;
    }
    .el-slider__button {
      width: 50px;
      height: 40px;
      border-radius: 0px;
      border: 0px;
      background: url("https://wlyxvpcdn.xiaoben365.com/wlyx/xbyx/2021/wu_1f3mqcmiac2kr111iaqjdt3s7h/arrowright.png")
        no-repeat;
      background-size: cover;
      width: 20px;
      height: 20px;
      &:hover {
        transform: scale(1);
      }
    }
    .ok .el-slider__button {
      background: url("https://wlyxvpcdn.xiaoben365.com/wlyx/xbyx/2021/wu_1f3mqcmhv167n14ccin9a283ipf/ok.png")
        no-repeat;
      background-size: cover;
      width: 20px;
      height: 20px;
    }
    .title {
      width: 100%;
      font-size: 32px;
      font-weight: 600;
      line-height: 24px;
      color: #303133;
      padding-bottom: 24px;
      border-bottom: 1px solid #909399;
      margin-bottom: 30px;
    }
    .el-button {
      width: 100%;
      height: 52px;
      border-radius: 4px;
      margin-bottom: 20px;
    }
    .el-button--success {
      background: #17c289;
    }
    .el-button + .el-button {
      margin-left: 0;
    }
    .el-input__inner {
      font-size: 12px;
    }
  }
  .footer {
    padding-top: 40px;
    text-align: center;
    font-size: 12px;
    font-family: PingFang SC;
    font-weight: 400;
    line-height: 17px;
    color: #909399;
    opacity: 1;
  }
}
</style>
